package com.atguigu.gulimall.order.inteceptor;

import com.atguigu.gulimall.common.vo.MemberVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class OrderInteceptor implements HandlerInterceptor {

    public static final ThreadLocal<MemberVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 当远程服务调用order服务时，不需要登录我们需要将其返回true
        String uri = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/order/order/status/**", uri);
        System.out.println("match:"+match);
        System.out.println("uri:"+uri);
        if(match){
            return true;
        }


        HttpSession session = request.getSession();
        System.out.println("执行了拦截器");
        MemberVo memberVo = (MemberVo) session.getAttribute("loginUser");
        System.out.println("memberVo:"+memberVo);
        if(memberVo != null){
            threadLocal.set(memberVo);
            return true;
        }else {
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }

}
